package top.oneit.jdownload.test;

import javafx.event.EventHandler;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

/**
 * @author lingkang
 * @date 2021/11/18
 */
public class MyStageCommon extends Stage {
    private String thisClassName=getClass().getName();
    public MyStageCommon() {
        super();
        // 创建窗口时加入
        MyApp.stages.put(thisClassName, this);
        // x 掉窗口时将它移除
        setOnCloseRequest(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent event) {
                MyApp.stages.remove(thisClassName);
            }
        });
    }

    @Override
    public void close() {
        super.close();
        MyApp.stages.remove(thisClassName);
    }

    /**
     * 获取对象,,注意返回空值
     */
    public <T> T getStage(Class<T> clazz) {
        if (!MyApp.stages.containsKey(clazz.getName()))
            return null;
        return (T) MyApp.stages.get(clazz.getName());
    }
}
